;
! function() {
	var layer = layui.layer,
		form = layui.form,
		upload = layui.upload;

	// 认证后用户个人基本信息需做隐藏处理

	// 当用户没认证或审核不通过时提交认证信息
	form.on("submit(saveAuth)", function() {
		// 判断证件图片是否有正确上传，分别有个人和企业
		if($("#idFrontImage").prop("src") == null && $("input:radio:checked").val() == 0 || $("#idFrontImage").prop("src") == "" && $("input:radio:checked").val() == 0) {
			layer.msg("请上传身份证正面照片");
			return false;
		} else if($("#idBackImage").prop("src") == null && $("input:radio:checked").val() == 0 || $("#idBackImage").prop("src") == "" && $("input:radio:checked").val() == 0) {
			layer.msg("请上传身份证反面照片");
			return false;
		}
		if($("input:radio:checked").val() == 1) {
			if($("#attachmentImage").prop("src") == null || $("#attachmentImage").prop("src") == "") {
				layer.msg("请上传营业执照");
				return false;
			}
		}

		// 提交请求
		$.ajax({
			url: "/secuser/authSave",
			type: "post",
			data: {
				"realName": $("#real-name").val(),
				"idCard":$("#id-number").val(),
				"type": $("input:radio:checked").val(),
				"companyName": $("#company-name").val(),
				"idCardFrontImgUrl": $("#idFrontImage").prop("src"),
				"idCardBackImgUrl": $("#idBackImage").prop("src"),
				"attachmentUrl": $("#attachmentImage").prop("src")
			},
			success: function(result) {
				layer.msg(result.msg);
				if(result.status == 0) {
					setTimeout("location = '/realNameAuth'", 2000);
				}
			}
		});

		return false;
	});

	// 身份证正面图片上传
	var uploadInst = upload.render({
		elem: '#idFrontImg-upload',
		url: '/secuser/idFrontUpload',
		accept: 'images',
		size: 5120,
		before : function(obj) {
			layer.load();
		},
		done: function(result) {
			layer.closeAll('loading');
			//上传完毕回调
			$("#idFrontImage").prop("src", result.data);
		},
		error: function() {
			layer.closeAll('loading');
			//请求异常回调
			layer.msg("上传失败");
		}
	});

	// 身份证背面图片上传
	var uploadInst = upload.render({
		elem: '#idBackImg-upload',
		url: '/secuser/idBackUpload',
		accept: 'images',
		size: 5120,
		before : function(obj) {
			layer.load();
		},
		done: function(result) {
			layer.closeAll('loading');
			//上传完毕回调
			$("#idBackImage").prop("src", result.data);
		},
		error: function() {
			layer.closeAll('loading');
			//请求异常回调
			layer.msg("上传失败");
		}
	});

	// 营业执照图片上传
	var uploadInst = upload.render({
		elem: '#attachmentImg-upload',
		url: '/secuser/attachmentUpload',
		accept: 'images',
		size: 5120,
		before : function(obj) {
			layer.load();
		},
		done: function(result) {
			layer.closeAll('loading');
			//上传完毕回调
			$("#attachmentImage").prop("src", result.data);
		},
		error: function() {
			layer.closeAll('loading');
			//请求异常回调
			layer.msg("上传失败");
		}
	});

	// 用户信息变量
	var authInfo;
	
	//自定义表单
	form.verify({
		username:function(value){
			var reg =  /^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$/;
			if (!reg.test(value)) {
				return "请输入正确的姓名！";
			}
		},
		identity:function(value){
			var reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
			if (!reg.test(value)) {
				return "请输入正确的身份证号码！";
			}
		}
	});

	// 当用户为个人时隐藏营业执照上传框
	form.on("radio(user-type)", function(data) {
		if(data.value == 1) {
			$("#company-name").attr("lay-verify","required");
			$("#auth_info .layui-form-item").eq(0).hide();
			$("#auth_info .layui-form-item").eq(1).hide();
			$("#auth_info hr").eq(0).hide();
			$("#auth_info hr").eq(1).hide();
			$(".enterprise-info").show();
		} else {
			$("#company-name").attr("lay-verify","");
			$(".enterprise-info").hide();
			$("#auth_info .layui-form-item").eq(0).show();
			$("#auth_info .layui-form-item").eq(1).show();
			$("#auth_info hr").eq(0).show();
			$("#auth_info hr").eq(1).show();
		}
	});
}();